package com.coderside.fragment.arithmetic;

/**
 * 演示二进制计算过程 演示简单的与运算 并打印运算结果 and 演示简单的或运算 并打印运算结果 or 演示简单的左平移运算 并打印运算结果
 * leftMove 演示简单的右平移运算 并打印运算结果 rightMove
 * 
 */
public class PrintBinaryString {

	public static void main(String[] args) {
		String binaryStra = toBinaryString(0xffffffff, 32);
		// System.out.println(binaryStra);
		// boolean[] bitArray=new boolean[1 << 27];
		System.out.println(binaryStra);
	}

	/**
	 * All possible chars for representing a number as a String
	 */
	final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
			'9', 'a', 'b', 'c', 'd', 'e', 'f' };

	/**
	 * Convert the integer to an unsigned number.
	 */
	private static String toUnsignedString(int i, int shift, int bit) {
		char[] buf = new char[bit];
		for (int j = 0; j < buf.length; j++) {
			buf[j] = '0';
		}
		int charPos = bit;
		int radix = 1 << shift;
		int mask = radix - 1;
		do {
			if (charPos <= 0) {
				System.err.println("max bit Exception");
				return "";
			}
			buf[--charPos] = digits[i & mask];
			i >>>= shift;
		} while (i != 0);

		return new String(buf);
	}

	private static void printBinaryString(int a, int bit) {
		String binaryStrb = toBinaryString(a, bit);
		printStr(binaryStrb);
	}

	public static void rightMove(int a, int shift, int bit) {
		System.out.println(a + "向右移" + shift + "位");
		String binaryStra = toBinaryString(a, bit);
		String binaryStrb = toBinaryString(a >> shift, bit);
		printStr(binaryStra);
		printStr(shift + "", ">> ");
		String line = "------------------------------------------------------------------------------------";
		System.out.println(line);
		printStr(binaryStrb);
	}

	public static void leftMove(int a, int shift, int bit) {
		System.out.println(a + "向左移" + shift + "位");
		String binaryStra = toBinaryString(a, bit);
		String binaryStrb = toBinaryString(a << shift, bit);
		printStr(binaryStra);
		printStr(shift + "", "<< ");
		String line = "------------------------------------------------------------------------------------";
		System.out.println(line);
		printStr(binaryStrb);
	}

	public static void and(int a, int b, int bit) {
		String binaryStra = toBinaryString(a, bit);
		String binaryStrb = toBinaryString(b, bit);
		String binaryStrc = toBinaryString(a & b, bit);
		printStr(binaryStra);
		printStr(binaryStrb, "AND");
		String line = "------------------------------------------------------------------------------------";
		System.out.println(line);
		printStr(binaryStrc);
	}

	public static void or(int a, int b, int bit) {
		String binaryStra = toBinaryString(a, bit);
		String binaryStrb = toBinaryString(b, bit);
		String binaryStrc = toBinaryString(a | b, bit);
		printStr(binaryStra);
		printStr(binaryStrb, "OR ");
		String line = "------------------------------------------------------------------------------------";
		System.out.println(line);
		printStr(binaryStrc);
	}

	private static void printStr(String str) {
		System.out.print("       ");
		for (int i = 0; i < str.length(); i++) {
			System.out.print(str.charAt(i) + " ");
		}
		System.out.println();
	}

	private static void printStr(String str, String andor) {
		System.out.print("  " + andor + "  ");
		for (int i = 0; i < str.length(); i++) {
			System.out.print(str.charAt(i) + " ");
		}
		System.out.println();
	}

	private static String toBinaryString(int a, int bit) {
		String str = toUnsignedString(a, 1, bit);
		return str;
	}

}
